<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 16-3-8
 * Time: 下午12:28
 */

namespace Admin\Controller;
use Think\Upload;
use Common\Model\CaozuologModel;
use Common\Model\ConfigModel;
use Common\Model\MemberModel;
class MemberController extends AdminController {
    public function _initialize(){
        parent::_initialize();
    }
    /**
     * 获取登录的管理员信息
     */
    public function getAdminByToken(){
    	$data = [
			'data'=>$this->user,
			'status'=>1,
    	];
    	$this->ajaxReturn($data);
    } 
    /**
     * 会员列表
     */
    public function index(){
    	// 用户名 身份证  真实姓名  手机号   注册时间  是否实名认证 锁定状态  锁定交易
    	$order = '';
    	$sort = I('sort');
    	if(!empty($sort)){
    		if(!in_array($sort, [1,2])){
    			$data = [
    			'status'=>-1,
    			'msg'=>'参数错误，排序参数错误'
    					];
    			$this->ajaxReturn($data);
    		}
    		$sort==1?$order = 'reg_time':$order = 'reg_time desc';
    			
    	}
    	$uid=I('uid');
		$username = I('username');
		$phone = I('userphone');
		$begin = I('begin');
		$last = I('last');
		$page = I('page');
		$num = I('num');
    	$admin = $this->user;
		$where = [];
		if(!empty($uid)){
			$where['m.member_id'] = $uid;
		}
		if(!empty($username)){
			$where['username'] = array('like','%'.$username.'%');
		}
		if(!empty($phone)){
			$where['phone'] = array('like','%'.$phone.'%');
		}
		$whereTime = $this->dotime($begin, $last);
		$where['reg_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$return = [];
		$pages = $this->DoPage($page, $num);
		$user = M('Member m')
		->where($where)
		->join('yang_user_relation as ur on ur.uid = m.member_id')
		->limit($pages['limit'],$pages['num'])
		->order($order)->select();
		 foreach ($user as $k=>&$v){
			$price=M('price')->find();
			$user[$k]['btc_yeji']=round($v['num1']*$price['price1'],2);
			$user[$k]['eth_yeji']=round($v['num2']*$price['price2'],2);
			$user[$k]['eos_yeji']=round($v['num3']*$price['price3'],2);
			$user[$k]['usdt_yeji']=round($v['num4']*$price['price4'],2);
		} 
		$count = M('Member m')->where($where)->count();
		$return = [
			'count'=>$count,
			'data'=>$user,
			'status'=>1
		];
		$this->ajaxReturn($return);
    }
    public function getCurrencyDetail(){
    	$uid=I('uid');
    	$list=M('member_money')
    		->alias('m')
    		->field('m.*,c.currency_mark')
    		->join('__CURRENCY__ as c on c.currency_id = m.currency_id')
    		->where('m.uid ='.$uid)
    		->select();
    	$return = [
    	'data'=>$list,
    	'status'=>1
    	];
    	$this->ajaxReturn($return);
    }
    public function getWalletDetail(){
    	$uid=I('uid');
    	$list=M('wallet')
    	->where('uid ='.$uid)
    	->find();
    	$return = [
    	'data'=>$list,
    	'status'=>1
    	];
    	$this->ajaxReturn($return);
    }
    /**
     * 赠送PAI币
     */
    public function addCoin(){
        $currency_id=I('currency_id');
        $type=I('type');
        $num=I('num');
        $uid=I('uid');
        $wallet=M('member_money')->where('uid ='.$uid.' and currency_id ='.$currency_id)->find();
        if(!$wallet){
            $data = [
                'status'=>-1,
                'msg'=>'参数错误'
        				];
            $this->ajaxReturn($data);
        }
        if($num<0){
            $data = [
                'status'=>-1,
                'msg'=>'数量不能小于0'
        				];
            $this->ajaxReturn($data);
        }
        if($type==1){
        	$r[]=M('Member_money')->where('uid ='.$uid.' and currency_id ='.$currency_id)->setInc('num',$num);
        	$r[]=$this->addFinance($uid, 1, '后台配币', $num, 1, $currency_id);
        	$r[]=$this->addAdminLog($uid, 1, $num, $currency_id,$this->user['admin_id']);
        }else if($type==2){
        	if($wallet['num']<$num){
        		$data = [
        		'status'=>-1,
        		'msg'=>'该用户钱包余额不足'
        				];
        		$this->ajaxReturn($data);
        	}
        	$r[]=M('Member_money')->where('uid ='.$uid.' and currency_id ='.$currency_id)->setDec('num',$num);
        	$r[]=$this->addFinance($uid, 2, '后台撤资', $num, 1, $currency_id);
        	$r[]=$this->addAdminLog($uid, 2, $num, $currency_id,$this->user['admin_id']);
        }
        if(in_array(false, $r)){
            $data = [
                'status'=>-1,
                'msg'=>'操作失败'
        				];
            $this->ajaxReturn($data);
        }else{
            $data = [
                'status'=>1,
                'msg'=>'操作成功'
        				];
            $M_LOG = new CaozuologModel();
            $content = '后台充值'.$uid.'的用户,数量为'.$num;
            $M_LOG->addLog($this->user['username'], $content);
            $this->ajaxReturn($data);
        }
         
    }
    public function getCurrencyList(){
    	$list=M('currency')->select();
    	$return = [
    	'data'=>$list,
    	'status'=>1
    	];
    	$this->ajaxReturn($return);
    }
    /**
     * 查询配币日志
     */
    public function getCoinLog(){
    	$order = '';
    	$sort = I('sort');
    	if(!empty($sort)){
    		if(!in_array($sort, [1,2])){
    			$data = [
    			'status'=>-1,
    			'msg'=>'参数错误，排序参数错误'
    					];
    			$this->ajaxReturn($data);
    		}
    		$sort==1?$order = 'a.add_time':$order = 'a.add_time desc';
    		 
    	}
    	$cointype=I('currency_id');
    	$type=I('type');
    	$uid=I('uid');
    	$phone=I('phone');
    	$begin = I('begin');
    	$last = I('last');
    	$page = I('page');
    	$num = I('num');
    	if(!empty($cointype)){
    		$where['a.cointype'] = $cointype;
    	}
    	if(!empty($type)){
    		$where['a.type'] = $type;
    	}
    	if(!empty($uid)){
    		$where['a.uid'] = $uid;
    	}
    	if(!empty($phone)){
    		$where['u.userPhone'] = array('like','%'.$phone.'%');
    	}
    	$whereTime = $this->dotime($begin, $last);
    	$where['a.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
    	$return = [];
    	$pages = $this->DoPage($page, $num);
    	$list= M('Admin_coin_log')
    	->alias('a')
    	->field('a.*,u.phone,u.username,c.currency_mark')
    	->join("LEFT JOIN yang_member AS u ON u.member_id = a.uid")
    	->join("__CURRENCY__ AS c ON c.currency_id = a.cointype")
    	->where($where)
    	->limit($pages['limit'],$pages['num'])
    	->order($order)
    	->select();
    	$count= M('Admin_coin_log')
    	->alias('a')
    	->join("LEFT JOIN yang_member AS u ON u.member_id = a.uid")
    	->join("__CURRENCY__ AS c ON c.currency_id = a.cointype")
    	->where($where)
    	->count();
    	$return = [
    	'count'=>$count,
    	'data'=>$list,
    	'status'=>1
    	];
    	$this->ajaxReturn($return);
    }
    /**
     * 生成四位数字
     */
    public function getFourNumber(){
    	for($i=0;$i<4;$i++) {
    		$fontsize = 6;
    	
    		$data = 'abcdefghijkmnpqrstuvwxy';
    		$fontcontent = substr($data, rand(0, strlen($data)-1), 1);
    		$captch_code .= $fontcontent;
    	}
    	return $captch_code;
    }
    /**
     * 封号
     */
    public function lockUser(){
    	$uid=I('uid');
    	$user=M('member')->where('member_id='.$uid)->find();
    	if(!$user){
    		$msg['status']=0;
    		$msg['msg']='参数错误';
    		$this->ajaxReturn($msg);
    	}
    	if($user['is_lock']==1){
    		$msg['status']=0;
    		$msg['msg']='该用户已被封号，不需重新操作';
    		$this->ajaxReturn($msg);
    	}
    		$r=M('member')->where('member_id='.$uid)->setField('is_lock',1);
	    	if($r){
	    		$msg['status']=1;
	    		$msg['msg']='封号成功';
	    		$M_LOG = new CaozuologModel();
	    		$content = '封停账号,ID为'.$uid;
	    		$M_LOG->addLog($this->user['username'], $content);
	    		$this->ajaxReturn($msg);
	    	}else{
	    		$msg['status']=0;       
	    		$msg['msg']='服务器繁忙，请稍后再试';
	    		$this->ajaxReturn($msg);
	    	}
    
    }
    /**
     * 解封
     */
    public function unlockUser(){
    	$uid=I('uid');
    	$user=M('member')->where('member_id='.$uid)->find();
    	if(!$user){
    		$msg['status']=0;
    		$msg['msg']='参数错误';
    		$this->ajaxReturn($msg);
    	}
    	if($user['is_lock']==0){
    		$msg['status']=0;
    		$msg['msg']='该用户状态正常，不需重新操作';
    		$this->ajaxReturn($msg);
    	}
    		$r=M('member')->where('member_id='.$uid)->setField('is_lock',0);
    		if($r){
    			$msg['status']=1;
    			$msg['msg']='解封成功';
    			$M_LOG = new CaozuologModel();
	    		$content = '解封账号,ID为'.$uid;
	    		$M_LOG->addLog($this->user['username'], $content);
    			$this->ajaxReturn($msg);
    		}else{
    			$msg['status']=0;
    			$msg['msg']='服务器繁忙，请稍后再试';
    			$this->ajaxReturn($msg);
    		}
    	
    }
    /**
     * 重置密码
     */
    public function resetPwd(){
    	$uid=I('uid');
    	$user=M('member')->where('member_id='.$uid)->find();
    	if(!$user){
    		$msg['status']=0;
    		$msg['msg']='参数错误';
    		$this->ajaxReturn($msg);
    	}
    	$newpwd=$this->config['reset_pass'];
    	$r=M('member')->where('member_id='.$uid)->setField('pwd',md5($newpwd));
    	if($r){
    		$msg['status']=1;
    		$msg['msg']='重置成功';
    		$M_LOG = new CaozuologModel();
    		$content = '重置账号密码,ID为'.$uid;
    		$M_LOG->addLog($this->user['username'], $content);
    		$this->ajaxReturn($msg);
    	}else{
    		$msg['status']=0;
    		$msg['msg']='服务器繁忙，请稍后再试';
    		$this->ajaxReturn($msg);
    	}
    }
    /**
     * 重置二级密码
     */
    public function resetTradePwd(){
    	$uid=I('uid');
    	$user=M('member')->where('member_id ='.$uid)->find();
    	if(!$user){
    		$msg['status']=0;
    		$msg['msg']='参数错误';
    		$this->ajaxReturn($msg);
    	}
    	$newpwd=$this->config['reset_pass_trade'];
    	$r=M('member')->where('member_id ='.$uid)->setField('pwdtrade',md5($newpwd));
    	if($r){
    		$msg['status']=1;
    		$msg['msg']='重置成功';
    		$M_LOG = new CaozuologModel();
    		$content = '重置账号二级密码,ID为'.$uid;
    		$M_LOG->addLog($this->user['username'], $content);
    		$this->ajaxReturn($msg);
    	}else{
    		$msg['status']=0;
    		$msg['msg']='服务器繁忙，请稍后再试';
    		$this->ajaxReturn($msg);
    	}
    }
}